<?php

include('config.php');


// table name
$tbl_name="user_info";

//get field values

$name=$_POST['name'];
$email=$_POST['email'];
$contact=$_POST['contact'];
$address=$_POST['address'];
$city=$_POST['city'];
$state=$_POST['state'];
$country=$_POST['country'];
$pincode=$_POST['pincode'];
$password=md5($_POST['password']);
$confirm_code = md5($_POST['email']);


// Create Mysqli object
$db = new mysqli($db_server,$db_user,$db_pass,$db_database);

// Create statement object
$stmt = $db->stmt_init();


// Create a prepared statement
if($stmt->prepare("INSERT INTO $tbl_name(name, email, password, contact, address, city, state, country, pincode, confirm_code) VALUES(?,?,?, ?,?,?,?,?,?,?)")) 
{
 
   
   // Bind your variable to replace the ?
    $stmt->bind_param('sssissssis', $name, $email, $password, $contact, $address, $city, $state, $country, $pincode, $confirm_code );
	
	  // Execute query // if suceesfully inserted data into database
   if( $stmt->execute())
   {
	   // Close statement object
		$stmt->close();
		
		
		// content of the mail
		$message= "<html>";
		$message.= "<body>";
		$message.="<p>Your Comfirmation link </p>";
		$message.="<p>Click on this link to activate your account </p>";
		$message.="<p><a href='http://localhost/e-library/confirmation.php?a=$confirm_code' target='_blank'>confirmation link</a></p>";
		
		$message .= "</html>";
		$message .= "</body>";
		
		//subject of the mail
		$subject="Your Comfirmation link for e-library account\r\n";
		
		
		include('mail-config.php');
		
			if(!$mail->Send())  // check if mail was sent or not
			 {
				  // if some error occurs and email is not sent, then delete the entry from the database
				$sql1="delete from $tbl_name where email='$email'";
				$result1=mysql_query($sql1);
				 
			  echo "Mailer Error: " . $mail->ErrorInfo;
			  header('Location: ../error.html');
			  
			} else {
				echo "error occured in insert query";
				
				header('Location: ../successfull-registration.php');
				
				
				
			  //echo "Registration Successfull!! An email regarding the verification of your email is sent to your email address. You need to activate your account .";
			}
			
		 
	}
	
	else{
	header('Location: ../error.html');
	}
	
}


?>

